package com.szqhyxj.youxiangju.biz.merchantHome.plan.adapter;

import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.PlanStatus;
import com.szqhyxj.youxiangju.biz.plan.RowItem;
import com.szqhyxj.youxiangju.biz.plan.RowsItem;
import com.szqhyxj.youxiangju.sys.components.LoadImageUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.http.Field;

/**
 * XiaoPanYu
 * on 2018/11/3
 */
public class PlanAdapter extends BaseAdapter {

    private List<RowItem> defaultSites;
    private int type;

    public ItemClickListener getListener() {
        return listener;
    }

    public void setListener(ItemClickListener listener) {
        this.listener = listener;
    }

    private ItemClickListener listener;

    public PlanAdapter(List<RowItem> defaultSites, int type) {
        this.type = type;
        this.defaultSites = defaultSites;

    }

    @Override
    public int getCount() {
        return defaultSites.size();
    }

    @Override
    public Object getItem(int i) {
        return defaultSites.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            if (type == PlanStatus.SUCCESS.getCode() || type == PlanStatus.RUNNING.getCode()) {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_plan_normal_item, null);
            } else {
                view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_plan_error_item, null);
            }
            ViewHolder viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        }
        RowItem rowsItem = defaultSites.get(i);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.title.setText(rowsItem.getBankName());
        String type = rowsItem.getType();
        if (type.equals("online")) {
            holder.transactionType.setImageResource(R.mipmap.kj);
        }
        if (type.equals("offline")) {
            holder.transactionType.setImageResource(R.mipmap.sb);

        }
        /*holder.billSum.setText(String.format("%.2f元", rowsItem.getTotalBillAmount()));
        holder.repaymentSum.setText(String.format("%.2f元", rowsItem.getRepaidAmount()));*/
        holder.billSum.setText(rowsItem.getTotalBillAmount());
        holder.repaymentSum.setText(rowsItem.getRepaidAmount());
        int progress = rowsItem.getProgress();
        holder.schedule.setProgress(progress);
        holder.progress.setText(progress + "%");
        LoadImageUtils.loadImage(holder.tubiao, rowsItem.getBankLogo());
        view.setOnClickListener(new OnBtnClickListener(i));
        return view;
    }

    public class OnBtnClickListener implements View.OnClickListener {
        private int i;

        public OnBtnClickListener(int i) {
            this.i = i;
        }

        @Override
        public void onClick(View view) {
            if (listener != null) {
                listener.onClickItem(i);
            }
        }
    }


    static class ViewHolder {
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.bill_sum)
        TextView billSum;
        @BindView(R.id.repayment_sum)
        TextView repaymentSum;
        @BindView(R.id.schedule)
        SeekBar schedule;
        @BindView(R.id.tubiao)
        ImageView tubiao;
        @BindView(R.id.progress)
        TextView progress;
        @BindView(R.id.transactionType)
        ImageView transactionType;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            schedule.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    return true;
                }
            });
        }
    }

    public interface ItemClickListener {
        void onClickItem(int i);
    }
}
